package com.woniuxy.charin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.charin.enumeration.Assert;
import com.woniuxy.charin.enumeration.BusinessCode;
import com.woniuxy.charin.enumeration.UserStatus;
import com.woniuxy.charin.model.dto.RoleInfoDto;
import com.woniuxy.charin.model.entity.Role;
import com.woniuxy.charin.model.entity.User;
import com.woniuxy.charin.model.entity.UserRole;
import com.woniuxy.charin.model.vo.UserInfoVo;
import com.woniuxy.charin.service.RoleService;
import com.woniuxy.charin.mapper.RoleMapper;
import com.woniuxy.charin.service.UserRoleService;
import com.woniuxy.charin.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author KOO
* @description 针对表【role】的数据库操作Service实现
* @createDate 2024-12-26 16:45:32
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{
        @Autowired
        RoleService roleService;
        @Autowired
        UserRoleService userRoleService;
        @Autowired
        UserService userService;
    /**
     * 根据状态和角色名称分页查询角色列表
     * @param roleInfoDto
     * @return
     */
    @Override
    public Page<Role> queryRole(RoleInfoDto roleInfoDto) {
        if(roleInfoDto.getStatus()==-1){roleInfoDto.setStatus(null);}
        Page<Role> rolePage = baseMapper.selectPage(new Page<>(roleInfoDto.getPageNum(), roleInfoDto.getPageSize()),
                Wrappers.lambdaQuery(Role.class)
                        .eq(roleInfoDto.getStatus() != null, Role::getStatus, roleInfoDto.getStatus())
                        .like(StringUtils.isNotBlank(roleInfoDto.getRoleName()), Role::getRoleName, roleInfoDto.getRoleName()));

        return rolePage;
    }

    @Override
    public List<User> getUser() {
        Role role = roleService.getOne(Wrappers.lambdaQuery(Role.class).eq(Role::getRoleName, "部门主管"));
        List<UserRole> list = userRoleService.list(Wrappers.lambdaQuery(UserRole.class).eq(UserRole::getRoleId, role.getRoleId()));
        Assert.error(list.isEmpty(), BusinessCode.NO_MANAGER);
        List<Integer> userId = list.stream().map(l -> l.getUserId()).collect(Collectors.toList());
        List<User> users = userService.listByIds(userId);
        return users;
    }

    @Override
    public void batchLock(Integer[] deleteIds) {
        baseMapper.batchUpdateStatus(deleteIds, 1);
    }

    @Override
    public void batchActivate(Integer[] deleteIds) {
        baseMapper.batchUpdateStatus(deleteIds, 0);
    }
}




